/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.javanile.chess.stone;

/**
 *
 * @author totalinnovation
 */
public class Uci {
    // UI TO ENGINE
    public final static int UCI			= 101;
    public final static int DEBUG_ON		= 102;
    public final static int DEBUG_OFF		= 103;
    public final static int ISREADY		= 104;
    public final static int SETOPTION		= 105;
    public final static int REGISTER		= 106;
    public final static int UCINEWGAME		= 107;
    public final static int POSITION		= 200;
    public final static int POSITION_FEN	= 201;
    public final static int POSITION_STARTPOS	= 202;
    public final static int GO			= 109;
    public final static int STOP		= 110;
    public final static int PONDERHIT		= 111;
    public final static int QUIT		= 112;

    // DEBUGGING
    public final static int _DEBUG		= 999;

    // ENGINE TO UI
    public final static String ID_NAME		= "id name ";
    public final static String ID_AUTHOR	= "id author ";
    public final static String UCIOK		= "uciok";
    public final static String READYOK		= "readyok";
    public final static String BESTMOVE		= "bestmove ";
    public final static String COPYPROTECTION	= "copyprotection";
    public final static String REGISTRATION	= "registration";
    public final static String INFO		= "info ";
    public final static String OPTION		= "option ";

    //
    public int	    action;
    public String   load;
    public String[] moves;

    public Uci(String str) {
	if (false) {
	} else if (str.startsWith("uci")) {
	    action  = UCI;
	} else if (str.startsWith("isready")) {
	    action  = ISREADY;
	} else if (str.startsWith("position fen ")) {
	    action  = POSITION_FEN;
	    load    = str.substring("position fen ".length());
	} else if (str.startsWith("position startpos")) {
	    action  = POSITION_STARTPOS;
	    load    = str.substring("position startpos".length()).trim();
	    if (load.startsWith("moves ")) {
		moves   = load.substring(6).split(" ");
	    } else {
		moves	= new String[0];
	    }
	} else if (str.startsWith("go ")) {
	    action  = GO;
	    load    = str.substring("go ".length());
	} else if (str.startsWith("stop")) {
	    action  = STOP;
	} else if (str.startsWith("quit")) {
	    action  = QUIT;
	} else if (str.equals("debug on")) {
	    action  = DEBUG_ON;
	} else if (str.equals("debug off")) {
	    action  = DEBUG_OFF;
	} else if (str.startsWith("--")) {
	    action  = _DEBUG;
	    load = str.substring("--".length()).trim();
	}
    }

    public boolean prosecute() {
	return action!=QUIT;
    }
}
